package cn.edu.glut.user.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;




import org.assertj.core.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import cn.edu.glut.entity.Student;
import cn.edu.glut.user.repository.StudentRepository;
import cn.edu.glut.user.service.StudentService;
import cn.edu.glut.vo.StudentToAdminVO;

/**
 * 
 * @author Caizhfy
 * @email caizhfy@163.com
 * @createTime 2017年12月9日
 * @description
 *
 */
@Service
public class StudentServiceImpl implements StudentService {
	final Logger logger = LoggerFactory.getLogger(StudentService.class);
	
	@Autowired
	private StudentRepository studentRepository;

	@Override
	public int save(Student s) {
		Student ns = studentRepository.save(s);
		if (!StringUtils.isEmpty(ns.getId()))
			return 1;
		return 0;
	}

	/*
	 * 该方法是执行动态查询的方法，可选择自由度不同的方法，
	 * 两种实现方式：SpringDataJPA官方方式（一般动态查询）
	 * 自己动手实现RepositoryIml根据拼接hql、sql方式（数据报表分组）
	 */
	/*@Override
	public Page<Student> list(Pageable pageable, final Boolean isSelectScuuess, final String majorId,
			final String username, final String nickname, final Boolean selection) {
		// 动态and查询
		Page<Student> page = studentRepository.findAll(new Specification<Student>() {
			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// TODO Auto-generated method stub
				List<Predicate> predicates = new ArrayList<Predicate>();
				if (!StringUtils.isEmpty(username)) {
					predicates.add(criteriaBuilder.equal(root.get("username"), username));
				}
				if (!StringUtils.isEmpty(nickname)) {
					predicates.add(criteriaBuilder.equal(root.get("nickname"), nickname));
				}
				if (!StringUtils.isEmpty(majorId)) {
					predicates.add(criteriaBuilder.equal(root.get("major").get("id"), majorId));
				}
				if (null != isSelectScuuess) {
					Join<Student, Selection> join = root.join("selections", JoinType.INNER);
					predicates.add(criteriaBuilder.equal(join.get("isSelection"), isSelectScuuess));
				}
				if(null != selection){
					predicates.add(criteriaBuilder.equal(root.get("selection"), selection));
				}
				return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		}, pageable);
		//实现这个方法的原因是将Page<Student> 转换成PageVO<StudentVO>,这达到这俩api看起来属性相同
		PageVO pagevo = PageVO.convertPage(new ConvertPageToPageVO(){
			@Override
			public PageVO convert(Page<?> page) {
				List<StudentVO> vos = new ArrayList<StudentVO>();
				List<Student> students = (List<Student>) page.getContent();
				for(Student s : students){
					//vos.add(new StudentVO(s));
				}
				return new PageVO(page,vos);
			}
		}, page);
		return page;
	}*/
	
	

	@Override
	@Transactional
	public int update(Student s) {
		Preconditions.checkNotNull(studentRepository.findOne(s.getId()),"该学生不存在");
		studentRepository.save(s);
		return 1;
	}

	@Override
	public Student findById(String id) {
		return studentRepository.findOne(id);
	}

	@Override
	public void delete(String id) {
		studentRepository.delete(id);
	}

	@Override
	public void deleteAll() {
		studentRepository.deleteAllInBatch();
	}

	@Override
	public void save(Collection<Student> studentList) {
		studentRepository.batchInsert(studentList);
//		studentRepository.save(studentList); //Spring 自带batch方法
	}

	@Override
	public Page<StudentToAdminVO> list(Pageable pageable, String majorId, Boolean isSelect, Boolean isSelectSuccess) {
		return studentRepository.findAllStudentToAdminVOPage(pageable, majorId, isSelect, isSelectSuccess);
	}

	@Override
	public StudentToAdminVO findStudentToAdminVOByUsername(String username) {
		return studentRepository.findStudentToAdminVOByUsername(username);
	}

	@Override
	public List<StudentToAdminVO> listBySubjectId(String subjectId) {
		List<StudentToAdminVO> voList = studentRepository.findAllBySubjectId(subjectId);
		for(StudentToAdminVO vo : voList){
			if(StringUtils.isEmpty(vo.getId()) && voList.size()==1){
				voList.remove(vo);
				break;
			}
		}
		return voList;
	}

}
